package org.lmj.dos.web.controller;

import com.google.common.collect.ImmutableMap;
import lombok.SneakyThrows;
import org.lmj.dos.common.bean.ServerResponse;
import org.lmj.dos.common.pojo.AddressEntity;
import org.lmj.dos.common.pojo.OrderDetailEntity;
import org.lmj.dos.common.pojo.OrderEntity;
import org.lmj.dos.common.pojo.ProductEntity;
import org.lmj.dos.common.utils.Snowflake;
import org.lmj.dos.mapper.AddressMapper;
import org.lmj.dos.mapper.OrderDetailMapper;
import org.lmj.dos.mapper.OrderMapper;
import org.lmj.dos.mapper.ProductMapper;
import org.lmj.dos.web.resultdo.OrderPayVo;
import org.lmj.dos.web.resultdo.OrderSaveDto;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@RestController
@RequestMapping
public class OrdersController {
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    AddressMapper addressMapper;
    @Autowired
    OrderDetailMapper orderDetailMapper;
    @Autowired
    ProductMapper productMapper;


    @GetMapping("/orders/getAll/{userId}")
   // public ServerResponse<OrdersVo.GetOrders> ordersList(String pageNum, Integer pageSize) throws IOException {
    public ServerResponse ordersList(@PathVariable("userId") Integer userId){
        List<OrderPayVo>orderPayVoList=new ArrayList<>();
        List<OrderEntity> orderEntities = orderMapper.selectByMap(ImmutableMap.of("user_id", userId));
        for (OrderEntity orderEntity:orderEntities){
            List<OrderDetailEntity> order_id = orderDetailMapper.selectByMap(ImmutableMap.of("order_id", orderEntity.getId()));
            Set<Integer> collect = order_id.stream().map(OrderDetailEntity::getProductId).collect(Collectors.toSet());
            List<ProductEntity> productEntityList = productMapper.selectBatchIds(collect);
            OrderPayVo orderPayVo = new OrderPayVo();
            BeanUtils.copyProperties(orderEntity,orderPayVo);
            orderPayVo.setProductEntities(productEntityList);
            orderPayVoList.add(orderPayVo);
        }
        return ServerResponse.success(orderPayVoList);
//        ClassPathResource classPathResource = new ClassPathResource("orders-list.json");
//        InputStream inputStream =classPathResource.getInputStream();
//        ObjectMapper objectMapper = new ObjectMapper();
//        OrdersVo.GetOrders getOrders = objectMapper.readValue(inputStream, OrdersVo.GetOrders.class);
//        return ServerResponse.success(getOrders);
    }

    @PostMapping("/orders")
    public ServerResponse addOrders(@RequestBody OrderSaveDto orderSaveDto) throws IOException {
        OrderEntity orderEntity = new OrderEntity();
        BeanUtils.copyProperties(orderSaveDto,orderEntity);
        long l = Snowflake.getInstance().get();
        orderEntity.setOrderNo(String.valueOf(l));
        orderMapper.insert(orderEntity);
        List<Integer> productIds = orderSaveDto.getProductIds();
        for (Integer aa:productIds){
            OrderDetailEntity orderDetailEntity = new OrderDetailEntity();
            orderDetailEntity.setProductId(aa);
            orderDetailEntity.setOrderId(orderEntity.getId());
            orderDetailMapper.insert(orderDetailEntity);
        }
        return ServerResponse.success(String.valueOf(l));
    }


    //订单支付
    @SneakyThrows
    @GetMapping("/orders/{orderPayNo}")
    public ServerResponse orderPay(@PathVariable("orderPayNo")String orderPayNo) {
        OrderEntity orderEntity = orderMapper.selectByMap(ImmutableMap.of("order_no", orderPayNo)).get(0);
        AddressEntity addressEntity = addressMapper.selectById(orderEntity.getAddressId());
        List<OrderDetailEntity> order_id = orderDetailMapper.selectByMap(ImmutableMap.of("order_id", orderEntity.getId()));
        Set<Integer> collect = order_id.stream().map(OrderDetailEntity::getProductId).collect(Collectors.toSet());
        List<ProductEntity> productEntityList = productMapper.selectBatchIds(collect);
        OrderPayVo orderPayVo = new OrderPayVo();
        BeanUtils.copyProperties(orderEntity, orderPayVo);
        orderPayVo.setAddressEntity(addressEntity);
        orderPayVo.setProductEntities(productEntityList);
        return ServerResponse.success(orderPayVo);
    }

    //更新订单状态
    @SneakyThrows
    @GetMapping("/orders/update/{orderId}")
    public ServerResponse update(@PathVariable("orderId")String orderId) {
        OrderEntity orderEntity1 = new OrderEntity();
        orderEntity1.setStatus(1);
        orderEntity1.setOrderNo(orderId);
        orderMapper.updateStatus(orderEntity1);
        return ServerResponse.success();
    }
}
